package com.smarthome.core.account.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShAccountDoorService;
import com.smarthome.core.account.AccountDoorService;
import com.smarthome.domain.bean.ShAccountDoor;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

@Service("accountDoorService")
public class AccountDoorServiceImpl implements AccountDoorService {

	@Resource
	private ShAccountDoorService shAccountDoorService;
	
	@Override
	public DataGrid queryDataGrid(ShAccountDoor accountDoor, PageHelper page) {
		return shAccountDoorService.queryDataGrid(accountDoor, page);
	}

	@Override
	public boolean ownerExist(Long doorId) {
		ShAccountDoor accountDoor=new ShAccountDoor();
		accountDoor.setDoorId(doorId);
		accountDoor.setOwner(1);
		
		List<ShAccountDoor> res=shAccountDoorService.queryShAccountDoorByCon(accountDoor);
		if(res.size()>0){
			return true;
		}
		
		return false;
	}

	@Override
	public boolean accountExist(ShAccountDoor accountDoor) {
		accountDoor.setOwner(null);
		List<ShAccountDoor> res=shAccountDoorService.queryShAccountDoorByCon(accountDoor);
		if(res.size()>0){
			return true;
		}
		return false;
	}

	@Override
	public void add(ShAccountDoor accountDoor) {
		shAccountDoorService.saveOrUpdate(accountDoor);
	}

	@Override
	public void delete(ShAccountDoor accountDoor) {
		shAccountDoorService.deleteByCondtion(accountDoor);
	}

	@Override
	public ShAccountDoor queryDataById(Long id) {
		
		return shAccountDoorService.selectEntryById(id);
	}

	@Override
	public ShAccountDoor queryDataByCondition(ShAccountDoor accountDoor) {
		List<ShAccountDoor> res = shAccountDoorService.queryShAccountDoorByCon(accountDoor);
		if(null != res && res.size()>0){
			return res.get(0);
		}
		return null;
	}

	@Override
	public List<ShAccountDoor> queryDataListByCondition(
			ShAccountDoor accountDoor) {
		return shAccountDoorService.queryShAccountDoorByCon(accountDoor);
	}
	
	@Override
	public void hasOtherHouse(ShAccountDoor accountDoor){
		ShAccountDoor query = new ShAccountDoor();
		query.setAccountId(accountDoor.getAccountId());
		query.setResidenceId(accountDoor.getResidenceId());
		query.setIsDefault(1);//默认房屋
		List<ShAccountDoor> doors = queryDataListByCondition(query);
		accountDoor.setIsDefault(2);//初始化非默认房屋
		if(doors != null && doors.size() <= 0){
			accountDoor.setIsDefault(1);//设置默认房屋
		}
	}
	
	@Override
	public void changeOtherHouseNotDefault(ShAccountDoor accountDoor,Long dataId){
		ShAccountDoor queryDoor = new ShAccountDoor();
		queryDoor.setAccountId(accountDoor.getAccountId());
		queryDoor.setResidenceId(accountDoor.getResidenceId());
		
		List<ShAccountDoor> doors = queryDataListByCondition(queryDoor);
		for(ShAccountDoor door : doors){
			if(door.getId().intValue() == dataId.intValue()){
				door.setIsDefault(1);//设置默认房屋
				shAccountDoorService.saveOrUpdate(door);
			}else{
				door.setIsDefault(2);//设置非默认房屋
				shAccountDoorService.saveOrUpdate(door);
			}
		}
	}

	@Override
	public ShAccountDoor getDefaultDoor(Long accountId, Long residenceId) {
		ShAccountDoor accountDoor = new ShAccountDoor();
        accountDoor.setAccountId(accountId);
        accountDoor.setIsDefault(1);
        accountDoor.setResidenceId(residenceId);
        //查询当前小区下用户的默认门牌信息
        accountDoor = queryDataByCondition(accountDoor);
        return accountDoor;
	}

	@Override
	public List<ShAccountDoor> getDetailList(Long accountId) {
		return shAccountDoorService.getDetailList(accountId);
	}
}
